﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace DSS_Simplex
{
    public partial class Form1 : Form
    {
        MySqlConnection connection;
        string ConnectionString = "Server=localhost;Database=dss;Uid=root;Pwd=;";
      
        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fr = new Form2();
            fr.Focus();
            fr.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FormConst fc = new FormConst();
            fc.Focus();
            fc.ShowDialog();
        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //akses dg2 ke dg3
            try
            {
                for (int x = 0; x < dataGridView2.Rows.Count; x++) 
                {
                    dataGridView4.RowCount = dataGridView2.Rows.Count;
                    dataGridView4.Rows[x].Cells[0].Value = dataGridView2.Rows[x].Cells[0].Value + "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //akses dg1 ke dg4
            try
            {
                for (int x = 0; x < dataGridView1.Rows.Count - 1; x++)
                {
                    dataGridView4.ColumnCount = dataGridView1.Rows.Count;
                    dataGridView4.Columns[x+1].Name = dataGridView1.Rows[x].Cells[0].Value + " ";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            int i;
            int j = 0;
            
            int varinit = dataGridView1.RowCount - 1;
            int constraint = dataGridView2.RowCount - 1;
            int vartotal = varinit + constraint;

            //max,min
            double[,] coef = new double[constraint, vartotal];
            double[,] bcoef = new double[constraint, 1];
            double[] ccoef = new double[vartotal];
            double[,] table = new double[constraint + 1, vartotal + 1];

            //dual
            double[,] coefd = new double[constraint, varinit];
            double[,] tcoef = new double[varinit,vartotal];
            double[,] tbcoef = new double[varinit, 1];
            double[] tccoef= new double[vartotal];
            double[,] ttable = new double[varinit + 1, vartotal + 1];
            int tbrs;
            int tklm;

            //bigM
            double[] arrMin = new double[constraint];
            double[] arrMax = new double[constraint];
            double[] arrEq = new double[constraint];
            int S1, S2, A1, A2;
            
            #region kondisi
            //kondisi
            int simMin = 0, simMax = 0, simEq = 0;
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty) 
                {
                    simMin++;
                }
            }
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty)
                {
                    simMax++;
                }
            }
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[3].Value) != string.Empty)
                {
                    simEq++;
                }
            }
            #endregion

            #region Button Maksimasi
            //kondisi maksimum
            if (radioButton1.Checked == true)
            {
                
                #region Simplex Max

                if (simMax == constraint && simMin == 0 && simEq == 0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }
                    //nilai awal S
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, j] = 0;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambahan variabel");
                    }

                    //buat variabel bantu
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambah 1");
                    }

                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");

                    }
                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                    }
                    for (j = varinit; j < vartotal; j++)
                    {
                        ccoef[j] = 0;
                    }
                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }


                    Mesin.CariMax(table);
                    //hasil
                    for (i = 0; i < constraint + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -table[0, 0]);

                }
                #endregion

                #region Dual Max
                else if (simMin == constraint && simMax == 0 && simEq == 0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);
                        }
                        Console.WriteLine(" matrik awal");
                    }
                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++)
                    {
                        for (j = 0; j < tbrs; j++)
                        {
                            tcoef[j, i] = coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");

                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, j] = 0;

                        }
                    //var s
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, constraint + i] = 1;
                        }
                    }
                    //matriks kendala
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(tcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi kendala
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }
                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    for (j = constraint; j < vartotal; j++)
                    {
                        tccoef[j] = 0;
                    }
                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        ttable[0, i] = tccoef[i - 1];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];

                        }

                    }
                    //tabel awal
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMin(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -ttable[0, 0]);

                }
                #endregion

                #region MMax
                else{
                    //bigM max
                    MessageBox.Show("big M");
                }
                #endregion
            }
            #endregion

            #region Button Minimasi
            
            if (radioButton2.Checked == true)
            {
                #region Simplex Min
                
                if (simMax == constraint && simMin == 0 && simEq ==0)
                {
                    //matriks awal
                    
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }

                    for (i = 0; i < constraint; i++)
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.WriteLine(coef[i, j]);
                        }
                    Console.WriteLine("tambah 1");
                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");

                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                    }
                    for (j = varinit; j < vartotal; j++)
                    {
                        ccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }

                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }
                    Mesin.CariMin(table);

                    //hasil
                    for (i = 0; i < constraint + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -table[0, 0]);

                }
                #endregion

                #region Dual Min

                else if (simMin == constraint && simMax ==0 && simEq==0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);
                        }
                        Console.WriteLine(" matrik awal");
                    }
                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++) 
                    {
                        for (j = 0; j < tbrs; j++) 
                        {
                            tcoef[j,i]= coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");
                    
                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, j] = 0;
                            
                        }
                    //var s
                    for (i = 0; i < varinit; i++) 
                    {
                        for (j = constraint; j < vartotal; j++) 
                        {
                            tcoef[i, constraint + i] = 1;
                        }
                    }
                    
                    //matriks kendala
                    for(i=0;i<varinit;i++)
                    {
                        for(j=0;j<vartotal;j++)
                        {
                            Console.Write(tcoef[i,j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi kendala
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }
                    
                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    for(j=constraint;j<vartotal;j++)
                    {
                        tccoef[j]=0;
                    }    
                    
                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++) 
                    {
                        ttable[0, i] = tccoef[i-1];
                    }
                    for (i = 1; i < varinit + 1; i++) 
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++) 
                    {
                        for (j = 1; j < vartotal + 1; j++) 
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];
                            
                        }
                        
                    }
                    //tabel awal
                    for (i = 0; i < varinit+1; i++)
                    {
                        for (j = 0; j < vartotal+1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMax(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -ttable[0, 0]);

                }

                #endregion

                #region MMin

                else
                {
                    //Msimplex min
                    //coef awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }
                    Console.WriteLine("koefisien awal c");
                    //isi array dgn 0
                    for (i = 0; i < constraint; i++) 
                    {
                        arrMin[i] = 0;
                        arrMax[i] = 0;
                        arrEq[i] = 0;
                    }
                    Console.WriteLine("isi 0");
                    //isi dgn nilai DGview2
                    for (i = 0; i < constraint; i++)
                    {
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty)
                        {
                            arrMin[i] = double.Parse(dataGridView2.Rows[i].Cells[1].Value + "");
                            Console.Write("min ");
                            Console.Write(arrMin[i]);
                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty)
                        {
                            arrMax[i] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                            Console.Write("max ");
                            Console.Write(arrMax[i]);
                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[3].Value) != string.Empty)
                        {
                            arrEq[i] = double.Parse(dataGridView2.Rows[i].Cells[3].Value + "");
                            Console.Write("Eq ");
                            Console.Write(arrEq[i]);
                        }
                    }
                    //count s1,s2,A1
                    S1 = 0;
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (arrMin[i] != 0)
                        {
                            S1++;
                        }
                    }
                    Console.Write("jumlah s1 = ");
                    Console.Write( S1);
                    S2 = 0;
                    A1 = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            S2++;
                            A1++;
                        }
                    }
                    Console.WriteLine("jumlah s2 = ");
                    Console.Write(S2);
                    Console.WriteLine("jumlah A1 =");
                    Console.WriteLine(A1);
                    A2 = 0;
                    for (i = 0; i < arrEq.Length; i++) 
                    {
                        if (arrEq[i] != 0)
                        {
                            A2++;
                        }
                    }
                    Console.WriteLine("jumlah A2 = ");
                    Console.WriteLine(A2);
                    //jml var tambahan
                    int x;
                    x = 0;
                    for (i = 0; i < constraint; i++) 
                    {
                        if (arrMax[i] != 0 && arrMin[i] != 0) 
                        {
                            x++;
                        }
                    }
                    Console.WriteLine("jumlah vartambahan = ");
                    Console.Write(x);
                    //tabel awal
                    int VarAll,MtableRow,MtableCol;
                    VarAll = S1+S2+A1+A2;
                    MtableRow = constraint+x;
                    MtableCol= varinit+VarAll;
                    double[,] Mcoef = new double[MtableRow, MtableCol];
                    double[,] Mbcoef = new double[MtableRow,1];
                    double[] Mccoef = new double[MtableCol];
                    double[,] Mtable = new double[MtableRow+1,MtableCol+1];
                    //isi tabel awal dgn 0
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0;j<MtableCol; j++)
                        {
                            Mcoef[i, j] = 0;
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //isi per DG
                    //max
                    Console.WriteLine("isi dg baru");
                    int a, b, c;
                    a = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[a, j] = coef[i, j];
                                Mbcoef[a, 0] = arrMax[i];
                            }
                            a++;
                        }
                    }    
                        
                    //tampilkan 
                    Console.WriteLine("max");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    b = a;
                    Console.Write(b);
                    for (i = 0; i < arrMin.Length; i++) 
                    {
                        if (arrMin[i] != 0)
                        {
                            for (j = 0; j < varinit; j++) 
                            {
                                Mcoef[b, j] = coef[i, j];
                                Mbcoef[b, 0] = arrMin[i];
                            } 
                            b++;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("min");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }

                    c = b;
                    for (i = 0; i < arrEq.Length; i++) 
                    {
                        if (arrEq[i] != 0) 
                        {
                            for (j = 0; j < varinit; j++) 
                            {
                                Mcoef[c, j] = coef[i, j];
                                Mbcoef[c, 0] = arrEq[i];
                            }
                            c++;
                        }
                    }
                    //isi dgn 1
                    for (i = 0; i < S2; i++)
                    {
                        for (j = varinit; j < varinit + S2; j++)
                        {
                            Mcoef[i, varinit + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    
                    //isi dgn -1 dan 1
                    int f, g;
                    f = 0;
                    g = 1;
                    for (i = S2; i < S1+S2; i++) 
                    {
                        Mcoef[i, varinit + S2 + f] = -1;
                        Mccoef[varinit + S2 + f] = 0;
                        
                        Mcoef[i, varinit + S2 + g] = 1;
                        Mccoef[varinit + S2 + g] = 1.0E30;
                        
                        f +=2;
                        g +=2;
                    }
                    
                    //isi sisanya dgn 1
                    for (i = S1 + S2; i < MtableRow; i++) 
                    {
                        for (j = varinit + S1 + S2 + A1; j < MtableCol; j++) 
                        {
                            Mcoef[i, varinit + A1 + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn -1 dan 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine("bcoef");
                    for (i = 0; i < MtableRow  ; i++)
                    {
                        Console.Write(Mbcoef[i, 0]);
                        
                    }

                    //koefisien fungsi tujuan
                    for (i = 0; i < varinit; i++) 
                    {
                        Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value+"");
                    }
                    for (i = varinit; i < varinit + S2; i++) 
                    {
                        Mccoef[i] = 0;
                    }
                    for (i = MtableCol - A2; i < MtableCol; i++) 
                    {
                        Mccoef[i] = 1.0E30;
                    }
                    //mccoef
                    Console.WriteLine("nilai Mccoef: ");
                    for (i = 0; i < MtableCol; i++)
                    {
                        Console.Write(Mccoef[i]);
                    }
                    //gabungkan menjadi Mtable
                    Mtable[0, 0] = 0;
                    for(i=1;i<Mccoef.Length+1;i++)
                    {
                        Mtable[0,i]=Mccoef[i-1];
                    }
                    for(i = 1; i < Mbcoef.Length+1; i++) 
                    {
                        Mtable[i, 0] = Mbcoef[i-1,0];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        for (j = 1; j < Mccoef.Length + 1; j++) 
                        {
                            Mtable[i, j] = Mcoef[i - 1, j - 1];
                        }
                    }
                    Console.WriteLine("mtable: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow+1; i++) {
                        for (j = 0; j < MtableCol+1; j++) {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Mesin.InnerProduct(ref Mtable, A1, S2, A2);
                    Console.WriteLine("mtable baru: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        
                    }
                    Mesin.CariMin(Mtable);
                    Console.WriteLine("mtable hasil search min: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai fungsi objektif: ", -Mtable[0, 0]);
                }
                #endregion
            }
            
            #endregion

        }


        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        
        public int GetArrLength(double[] Arr) 
        {
            int S, i;
            S = 0;
            for (i = 0; i < Arr.Length; i++)
            {
                if (Arr[i] != 0) 
                {
                    S = S + 1;
                }
            }
            return S;
            
        }

        private void saveAsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveAs saveAs = new SaveAs();
            saveAs.Focus();
            saveAs.ShowDialog();

            connection = new MySqlConnection(ConnectionString);
            //sql command object

            
            MySqlCommand cmd1 = connection.CreateCommand();
            cmd1.CommandText = "SELECT MAX(id_proj) FROM project";
            connection.Open();
            int id_project = ((int)cmd1.ExecuteScalar());
            Console.WriteLine(id_project);
            
            
            int i;
            for (i = 0; i < dataGridView1.RowCount-1; i++)
            {
                
                string nama_var = dataGridView1.Rows[i].Cells[0].Value + "";
                double koef_var = double.Parse(dataGridView1.Rows[i].Cells[1].Value+"");
                double min_var = double.Parse(dataGridView1.Rows[i].Cells[2].Value + ""); ;
                double max_var = double.Parse(dataGridView1.Rows[i].Cells[3].Value + ""); ;
                string deskripsi = dataGridView1.Rows[i].Cells[4].Value + "";
                
                //save variable
                string cmdText = "insert into variable (nama_var,koef_var, min_var, max_var, deskripsi, id_proj) VALUES (@nama_var, @koef_var, @min_var, @max_var, @deskripsi, @id_proj)";
                MySqlCommand cmd = new MySqlCommand(cmdText, connection);
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.Add("@nama_var", nama_var);
                cmd.Parameters.Add("@koef_var", koef_var);
                cmd.Parameters.Add("@min_var", min_var);
                cmd.Parameters.Add("@max_var", max_var);
                cmd.Parameters.Add("@deskripsi", deskripsi);
                cmd.Parameters.Add("@id_proj", id_project);
                cmd.ExecuteNonQuery();
            } MessageBox.Show("Variabel disimpan");
            
            
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                
                string nama_const = dataGridView2.Rows[i].Cells[0].Value + "";
                double min_const = double.Parse(dataGridView2.Rows[i].Cells[1].Value + "");
                double max_const = double.Parse(dataGridView2.Rows[i].Cells[2].Value + ""); ;
                double equal = double.Parse(dataGridView2.Rows[i].Cells[3].Value + ""); ;
                
                //save constraint
                string cmdText2 = "insert into `constraint` (nama_const, min_const, max_const, equal, id_proj) VALUES (@nama_const, @min_const, @max_const, @equal, @id_proj)";
                MySqlCommand cmd2 = new MySqlCommand(cmdText2, connection);
                cmd2.CommandType = CommandType.Text;

                cmd2.Parameters.Add("@nama_const", nama_const);
                cmd2.Parameters.Add("@min_const", min_const);
                cmd2.Parameters.Add("@max_const", max_const);
                cmd2.Parameters.Add("@equal", equal);
                cmd2.Parameters.Add("@id_proj", id_project);
                cmd2.ExecuteNonQuery();
            } MessageBox.Show("constraint disimpan");
            connection.Close();
            
        }


       
    }
}
